package com.js.rtu.fragment.first;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.js.rtu.R;
import com.js.rtu.RTUApplication;
import com.js.rtu.base.BaseFragment;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.util.UUID;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.OnClick;
import cn.wandersnail.bluetooth.ConnectCallback;
import cn.wandersnail.bluetooth.Connection;


/**
 * 扫一扫
 * Created by wz on 2020/4/26.
 */
public class QRResultFragment extends BaseFragment {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.ll_debug)
    LinearLayout llDebug;
    @BindView(R.id.tv_mac)
    TextView tvMac;
    private Connection connection;
    public static UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    public static QRResultFragment newInstance(String qrcode) {
        Bundle args = new Bundle();
        QRResultFragment fragment = new QRResultFragment();
        args.putString("msg",qrcode);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_qrresult;
    }

    @Override
    protected void initView() {
        initToolbarNav(toolbar);
        toolbarTitle.setText("连接设备");
        toolbar.inflateMenu(R.menu.menu_connect);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                connect();

                return false;
            }
        });
        Bundle bundle = getArguments();
        if (bundle==null) {
            return;
        }
        String info= bundle.getString("msg");
        tvMac.setText(info);
        if (!isValidMac(info)){
            BasePopupView popupView = new XPopup.Builder(_mActivity)
                    .asConfirm("mac地址不符合", "",
                            "", "确定",
                            new OnConfirmListener() {
                                @Override
                                public void onConfirm() {
                                    _mActivity.onBackPressed();
                                }
                            }, null, true);
            popupView.show();
            return;
        }
//        String  info="FC:18:3C:37:F0:CF";
//        String  info="85:58:0E:16:5E:BC";
        connection=   RTUApplication.getInstance().getConnection(info);
    }
    private boolean isValidMac(String macStr) {

        if (macStr == null || macStr.equals("")) {
            return false;
        }
        String macAddressRule = "([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}";
        // 这是真正的MAC地址；正则表达式；
        if (macStr.matches(macAddressRule)) {
            return true;
        } else {
            return false;
        }
    }
    public void connect() {
        if (connection == null){
            add("连接失败，找不到设备", llDebug);
            return;
        }

        if (connection.isConnected()) {
            add("已连接", llDebug);
            return;
        }

        add("连接中", llDebug);

        connection.connect(SPP_UUID, new ConnectCallback() {
            @Override
            public void onSuccess() {
                add("连接成功", llDebug);


            }

            @Override
            public void onFail(@NonNull String errMsg, @Nullable Throwable e) {

                add(errMsg, llDebug);
                add("连接断开", llDebug);
                //                RTUApplication.getInstance().releaseConnection();
            }
        });
    }




    @OnClick({R.id.tv_clean})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_clean: {
                llDebug.removeAllViews();
                break;
            }
        }
    }
}
